package com.tianfeng.news.ui.activity.other;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.tianfeng.news.R;
import com.tianfeng.news.globle.ConstantUrl;
import com.tianfeng.news.model.bean.UserBean;
import com.tianfeng.news.net.HttpView;
import com.tianfeng.news.net.OkHttpHelper;
import com.tianfeng.news.net.api.ApiSMSSend;
import com.tianfeng.news.net.api.user.userInfo.ApiBindPhoneNumber;
import com.tianfeng.news.net.api.user.userInfo.ApiOnlyBindPhoneNumber;
import com.tianfeng.news.net.bean.ResEmpty;
import com.tianfeng.news.ui.activity.base.BaseActivity;
import com.tianfeng.news.ui.dialog.InsertUserWithDialogFragment;
import com.tianfeng.news.ui.dialog.invite.InviteFriendShareDialogFour;
import com.tianfeng.news.utils.ToastUtil;
import com.tianfeng.news.utils.ValidUtils;
import com.zhxu.library.http.HttpManager;
import com.zhxu.library.listener.HttpOnNextListener;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public class HomeUserWithActivity extends BaseActivity implements HttpView, View.OnClickListener {

    @BindView(R.id.tv_binding_phone)
    TextView tvPhone;
    @BindView(R.id.iv_binding_phone)
    ImageView iv_phone;


    @BindView(R.id.tv_share_friend)
    TextView tv_share_friend;
    @BindView(R.id.iv_share_friend)
    ImageView iv_share_friend;

    @BindView(R.id.tv_withdraw)
    TextView tv_withdraw;

    @BindView(R.id.tv_back)
    ImageView tv_back;

    private boolean isBindingPhone = false;
    @Override
    public int getContentLayout() {
        return R.layout.activity_home_user_with;
    }


    @Override
    protected void initView(View rootView, Bundle savedInstanceState) {
        setStatusBarImmersive(null);
        isGetUserPhone();
        if (getUserBean() != null) {
            String userid = getUserBean().getUserid();
            mShareDialog = new InviteFriendShareDialogFour(this, null,userid);
        }
        tv_share_friend.setOnClickListener(this);
        tv_withdraw.setOnClickListener(this);
        tv_back.setOnClickListener(this);
    }

    private void isGetUserPhone() {
        UserBean userBean = getUserBean();
        if (userBean != null) {
            String userid = userBean.getUserid();
            if (!TextUtils.isEmpty(userid)) {
                HashMap<String, String> params = new HashMap<>();
                params.put("id", userid);
                OkHttpHelper.getAsyncHttp(this, 10001, params, ConstantUrl.REQUEST_GETUSERPHONE, this);

            }
        }
    }

    @Override
    public void onSuccessResult(int requestCode, int returnCode, String returnMsg, String returnData, Map<String, String> paramsMaps) {
        switch (requestCode){
            case 10001:
                tvPhone.setVisibility(View.VISIBLE);
                if (returnMsg.indexOf("未绑定手机号")!=-1){
                    tvPhone.setTextColor(ContextCompat.getColor(this,R.color.color_cc));
                    tvPhone.setBackgroundResource(R.drawable.shape_bg_edit_black);
                    tvPhone.setOnClickListener(this);
                    tvPhone.setText("未完成");
                    iv_phone.setBackgroundResource(R.drawable.black_unbinding);
                    isBindingPhone = false;
                }else {
                    tvPhone.setTextColor(ContextCompat.getColor(this,R.color.color_blue));
                    tvPhone.setBackgroundResource(R.drawable.shape_bg_edit_blue);
                    tvPhone.setOnClickListener(null);
                    tvPhone.setText("已完成");
                    iv_phone.setBackgroundResource(R.drawable.blue_binding);
                    isBindingPhone = true;
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("TAG_分享到朋友圈","onResume");
        boolean share = mShareDialog.isShare();
        if (share){
            tv_share_friend.setTextColor(ContextCompat.getColor(this,R.color.color_blue));
            tv_share_friend.setBackgroundResource(R.drawable.shape_bg_edit_blue);
            tv_share_friend.setOnClickListener(null);
            tv_share_friend.setText("已完成");
            iv_share_friend.setBackgroundResource(R.drawable.blue_binding);
        }
    }

    @Override
    public void onFailure(int callbackId, String data) {

    }
    private InviteFriendShareDialogFour mShareDialog;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_binding_phone:
                doShowAddPhoneNumber();
                break;
            case R.id.tv_share_friend:
                mShareDialog.setFriendGone();
                mShareDialog.setQRGone();
                mShareDialog.show();

//                showShareDialog();
                break;
            case R.id.tv_withdraw:
                if (!isBindingPhone){
                    ToastUtil.showToast("请绑定手机号！");
                    return;
                }

                boolean share = mShareDialog.isShare();
                if (!share){
                    ToastUtil.showToast("请分享到朋友圈！");
                    return;
                }
                InsertUserWithDialogFragment dialogFragment = new InsertUserWithDialogFragment();
                Bundle bundle = new Bundle();
                bundle.putString("userId", getUserBean().getUserid());
                dialogFragment.setArguments(bundle);
                dialogFragment.show(getSupportFragmentManager(), "InsertUserWith");
                break;
        }
    }

//    public void showShareDialog() {
//
//    }


    private void doShowAddPhoneNumber() {

        final HashMap<String ,String > paramsMap=new HashMap<>();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("绑定手机号");
        View view = View.inflate(this, R.layout.dialog_bind_phone_number, null);

        final EditText mEtPhone = (EditText) view.findViewById(R.id.et_phone);
        final EditText mEtPhoneCode = (EditText) view.findViewById(R.id.et_phone_number_code);
        final TextView tvSendCode = (TextView) view.findViewById(R.id.tv_send_proof);
        tvSendCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = mEtPhone.getText().toString().trim();
                if(!ValidUtils.isMobileNO(trim)){
                    ToastUtil.showToast("请输入正确的手机号!");
                }

                //发送短信的一个方法；
                ApiSMSSend.defaultSend(HomeUserWithActivity.this,tvSendCode,trim,ApiSMSSend.BIND_PHONE_CODE);
            }
        });
        builder.setView(view);

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.setPositiveButton("确定", null);
        final AlertDialog alertDialog = builder.create();

        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                //这里我们对进行一个验证的操作；

                Button button =  alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                button.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        //这里我们对输入的内容进行一个校验操作；
                        String trim = mEtPhone.getText().toString().trim();
                        if(!ValidUtils.isMobileNO(trim)){
                            ToastUtil.showToast("请输入正确的手机号!");
                            return;
                        }
                        String phoneCode = mEtPhoneCode.getText().toString().trim();
                        if(TextUtils.isEmpty(phoneCode)){
                            ToastUtil.showToast("验证码不能为空!");
                            return;
                        }

                        paramsMap.put(ApiBindPhoneNumber.PHONE_NUMBER,trim);
                        paramsMap.put(ApiBindPhoneNumber.PHONE_CODE,phoneCode);
                        //这里实现绑定手机号的逻辑
                        bindPhoneNumber(paramsMap,alertDialog);
                    }
                });
            }
        });
        alertDialog.show();
    }
    /**
     * 这里要执行绑定手机号的逻辑；
     */
    private void bindPhoneNumber(HashMap<String,String> paramsMap, final AlertDialog alertDialog) {

        ApiOnlyBindPhoneNumber apiBindPhoneNumber=new ApiOnlyBindPhoneNumber(new HttpOnNextListener<ResEmpty>() {
            @Override
            public void onNext(ResEmpty userStr) {

                //这表示绑定成功；
                ToastUtil.showToast("绑定成功！");
                //这里是需要将绑定给移除的；
                alertDialog.dismiss();

                tvPhone.setTextColor(ContextCompat.getColor(HomeUserWithActivity.this,R.color.color_blue));
                tvPhone.setBackgroundResource(R.drawable.shape_bg_edit_blue);
                tvPhone.setOnClickListener(null);
                tvPhone.setText("已完成");
                iv_phone.setBackgroundResource(R.drawable.blue_binding);
                isBindingPhone = true;
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                //这里表示绑定出错
            }
        },this,paramsMap);
        HttpManager.getInstance().doHttpDeal(apiBindPhoneNumber);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("TAG_分享","resultCode="+resultCode+"；requestCode="+requestCode);
        if (resultCode == Activity.RESULT_OK){
            switch (requestCode){
                case 1000:
                    tv_share_friend.setTextColor(ContextCompat.getColor(this,R.color.color_blue));
                    tv_share_friend.setBackgroundResource(R.drawable.shape_bg_edit_blue);
                    tv_share_friend.setOnClickListener(null);
                    tv_share_friend.setText("已完成");
                    iv_share_friend.setBackgroundResource(R.drawable.blue_binding);
                    break;
            }
        }
    }
}
